﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Common;
using log4net;
using AutoFacUtils;
using CashFlow.Web.Def;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Web.Service;
using CashFlow.Web.Models;
using CashFlow.Web.MyActionResult;
using CashFlow.Web.Common;

namespace CashFlow.Web.Controllers
{
    public class AppointmentController : Controller
    {
        private static ILog _logger = LogManager.GetLogger(typeof(AppointmentController));
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IAppointmentService GetAppointmentService() { return AutoFacHelper.Get<IAppointmentService>(); }

        // GET: Appointment
        public ActionResult Index()
        {
            var service = GetUserService();
            var entity = service.GetLoginUser();
            ViewBag.SelfName = entity.Name;

            return View();
        }

        private bool IsAppointmentTime()
        {
            var date = DateTime.Now;
            if ((date.DayOfWeek == DayOfWeek.Tuesday ||
                date.DayOfWeek == DayOfWeek.Wednesday ||
                date.DayOfWeek == DayOfWeek.Thursday ||
                date.DayOfWeek == DayOfWeek.Friday) &&
                (date.Hour >= 9 && date.Hour <= 18))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        [HttpPost]
        public ActionResult DoAppointment(string appointmentType)
        {
            bool bSuccess = false;
            string msg = "";
            if (appointmentType == "3-9k" || appointmentType == "9-15k")
            {
                if (IsAppointmentTime())
                {
                    var userService = GetUserService();
                    var ui = userService.GetLoginUser();
                    var appointmentService = GetAppointmentService();
                    var result2 = appointmentService.DoAppointment(ui.Id, appointmentType);
                    bSuccess = result2 == EAppointmentResult.Success;
                    msg = EnumTool.GetDescription<EAppointmentResult, int>(result2);
                }
                else
                {
                    msg = "不在预约时间范围内！";
                }
            }
            else
            {
                msg = "预约类型错误！";
            }

            return MyCommonTool.CreateOperationResult(
                    bSuccess
                    , msg
                    , ""
            );
        }

        public ActionResult MyAppointmentList()
        {
            var userService = GetUserService();
            var ui = userService.GetLoginUser();
            var appointmentService = GetAppointmentService();
            var ls = appointmentService.QueryAllByUserId(ui.Id);
            var ls2 = ls.Select(x => new AppointmentItemInfoModel
            {
                Name = x.UserInfo.Name,
                Time = x.AppointmentInfo.Time,
                Type = x.AppointmentInfo.Type
            }).OrderByDescending(x => x.Time).ToList();

            ViewBag.List = ls2;

            return View();
        }

        public ActionResult TeamAppointmentList()
        {
            if (IsAppointmentTime())
            {
                string date = DateTime.Now.ToString("yyyy-MM-dd");
                var userService = GetUserService();
                var ui = userService.GetLoginUser();
                var appointmentService = GetAppointmentService();
                var ls = appointmentService.QueryTeamAppointmentUsers(ui.Id, date);
                ViewBag.Data = ls;
            }
            else
            {
                ViewBag.Msg = "不在预约时间范围内！";
            }

            return View();
        }

        public ActionResult TeamNotAppointmentList()
        {
            if (IsAppointmentTime())
            {
                string date = DateTime.Now.ToString("yyyy-MM-dd");
                var userService = GetUserService();
                var ui = userService.GetLoginUser();
                var appointmentService = GetAppointmentService();
                var ls = appointmentService.QueryTeamNotAppointmentUsers(ui.Id, date);
                ViewBag.Data = ls;
            }
            else
            {
                ViewBag.Msg = "不在预约时间范围内！";
            }

            return View();
        }
    }
}